#include "glText.h"

#include <stdio.h>
#include <gl/glu.h>

glText::glText(int w, int h)
{
	m_screenHeight = h;
	m_screenWidth = w;
}

void glText::buildFont(HDC ghdc)
{
	HFONT	font;				// Windows Font ID
	HFONT	oldfont;			// Used For Good House Keeping

	m_base = glGenLists(96);	// Storage For 96 Characters

	font = CreateFont(-16,  		// Height Of Font
		0,					      	// Width Of Font
		0,						    // Angle Of Escapement
		0,							// Orientation Angle
		FW_BOLD,					// Font Weight
		FALSE,						// Italic
		FALSE,					    // Underline
		FALSE,				    	// Strikeout
		ANSI_CHARSET,		  		// Character Set Identifier
		OUT_TT_PRECIS,				// Output Precision
		CLIP_DEFAULT_PRECIS,		// Clipping Precision
		ANTIALIASED_QUALITY,		// Output Quality
		FF_DONTCARE|DEFAULT_PITCH,	// Family And Pitch
		"Courier New");				// Font Name

	oldfont = (HFONT)SelectObject(ghdc, font);	// Selects The Font We Want
	wglUseFontBitmaps(ghdc, 32, 96, m_base);	// Builds 96 Characters Starting At Character 32
	SelectObject(ghdc, oldfont);				// Selects The Font We Want
	DeleteObject(font);							// Delete The Font
}

void glText::print(float x, float y, const char *fmt, ...)
{
	char		text[256];			// Holds Our String
	va_list		ap;					// Pointer To List Of Arguments

	if (fmt == NULL)				// If There's No Text
		return;						// Do Nothing

	va_start(ap, fmt);				// Parses The String For Variables
	vsprintf_s(text, fmt, ap);		// And Converts Symbols To Actual Numbers
	va_end(ap);						// Results Are Stored In Text

	screenPosition(x, y);			// Set the screen position of the text

	glPushAttrib(GL_LIST_BIT);						// Pushes The Display List Bits
	glListBase(m_base - 32);						// Sets The Base Character to 32
	glCallLists((GLsizei)strlen(text), GL_UNSIGNED_BYTE, text);// Draws The Display List Text
	glPopAttrib();									// Pops The Display List Bits
}


void glText::killFont(void)
{
	glDeleteLists(m_base, 96);					// Delete All 96 Characters
}

void glText::screenPosition(float x, float y )
{
	// Before we create a new view port, we need to save the current one we have.
	// This saves our transform (matrix) information and our current viewport information.
	// At the end of this function we POP it back.
	glPushAttrib( GL_TRANSFORM_BIT | GL_VIEWPORT_BIT );

	glMatrixMode( GL_PROJECTION );	// Set our matrix to our projection matrix
	glPushMatrix();					// Push on a new matrix to work with
	glLoadIdentity();				// reset the matrix
	glMatrixMode( GL_MODELVIEW );	// Set our matrix to our model view matrix
	glPushMatrix();					// Push on a new matrix to work with
	glLoadIdentity();

	gluOrtho2D (0,  m_screenWidth, 0,  m_screenHeight);

	glRasterPos4f( x, y, 0, 1 );

	glPopMatrix();					// Pop the current modelview matrix off the stack
	glMatrixMode( GL_PROJECTION );	// Go back into projection mode
	glPopMatrix();					// Pop the projection matrix off the stack

	glPopAttrib();
}


